package me.yiyou.meihua

import FindBelongToPage
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.google.gson.Gson
import me.yiyou.meihua.data.SaveBean
import me.yiyou.meihua.ui.page.dateqiuga.DateQiGuaPage
import me.yiyou.meihua.ui.page.describe.DescribePage
import me.yiyou.meihua.ui.page.itemsnumber.ItemNumber
import me.yiyou.meihua.ui.page.main.MainPage
import me.yiyou.meihua.ui.page.memory.MemoryPage
import me.yiyou.meihua.ui.page.random.RandomPage
import me.yiyou.meihua.ui.page.random.RandomPrePage
import me.yiyou.meihua.ui.page.record.RecordPage
import me.yiyou.meihua.ui.page.sixlines.SixLineViewModel
import me.yiyou.meihua.ui.page.sixlines.SixLinesCreatePage
import me.yiyou.meihua.ui.page.sixlines.SixLinesPage
import me.yiyou.meihua.ui.page.study.StudyPage
import me.yiyou.meihua.ui.screen.Screen
import me.yiyou.meihua.ui.theme.MeihuaTheme
import java.net.URLDecoder

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MeihuaTheme {

                // nav controller
                val navController = rememberNavController()

                val sixLineViewmodel : SixLineViewModel = viewModel()
                // nav 导航容器
                NavHost(
                    navController = navController,
                    startDestination = Screen.Main.route    // 定义初始页面
                ) {
                    // 定义页面
                    composable(Screen.Main.route) { MainPage(navController) }   // 首页
                    composable(Screen.Memory.route) { MemoryPage() }   // 记忆
                    composable(Screen.Study.route) { StudyPage() } // 学习
                    composable(Screen.DateQiGua.route) { DateQiGuaPage() } // 日期起卦
                    composable(Screen.Describe.route) { DescribePage() } // 说明页面
                    composable(Screen.Record.route) { RecordPage() }  // 记录页面
                    composable(Screen.RandomPre.route) { RandomPrePage(navController) }  // 随机起卦前置页面
                    composable(
                        "${Screen.Random.route}/{saveBean}",
                        arguments = listOf(navArgument("saveBean") {
                            type = NavType.StringType
                        })
                    ) { item ->
                        val encodedJson = item.arguments?.getString("saveBean")
                        val json = URLDecoder.decode(encodedJson, "UTF-8")
                        val save = Gson().fromJson(json, SaveBean::class.java)
                        RandomPage(saveBean = save)
                    }  // 随机起卦页面
                    composable(Screen.ItemNumber.route) { ItemNumber() }
                    composable(Screen.FindBelongTo.route) { FindBelongToPage() }  // 随机起卦页面
                    composable(Screen.SixLines.route) {
                        SixLinesPage(navController = navController, viewModel = sixLineViewmodel) }  // 六爻起卦
                    composable(Screen.SixLinesCreate.route) { SixLinesCreatePage(sixLineViewmodel) }   // 六爻卦象生成
                }
            }
        }
    }
}
